#include <iostream>
#include "memory_bit_stream.h"
#include "robot_object.h"

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <memory.h>
#include <stdlib.h>
#include <unistd.h>

#define FIFO_SEVER "fifo_server"

//当前目录下创建有名管道fifo, 进行模拟数据的收发
int main() {
    std::cout << "Hello, World!" << std::endl;

    char buffer[1500] = {};

    int fd;
    RobotObject wobject(1, 1, 's');
    RobotObject robject;

    OutputMemoryBitStream os;
    InputMemoryBitStream  is(buffer, 1500);

    if((mkfifo(FIFO_SEVER,0777) < 0) && (errno != EEXIST)){
        printf("cannt create fifosever!\n");
         exit(0);
     }

     fd = open(FIFO_SEVER,O_RDWR);
     if(fd == -1){
         printf("open failed --> fd:%d \n", fd);
     }else{
         printf("open ok --> fd:%d\n", fd);
     }

    printf("open ok --> fd:%d\n", fd);

     wobject.pos_list_.push_back(pos(100, 35, 1000));
     wobject.pos_list_.push_back(pos(101, 36, 2000));
     wobject.pos_list_.push_back(pos(102, 37, 3000));

     //对象进行序列化.
     wobject.write(os);

     int  send_count =  write(fd, os.get_buffer(), os.get_byte_length());

     std::cout << "send_count:" << send_count << std::endl;

     int recv_count = read(fd, buffer, 1500);

     //对象反序列化.
     robject.read(is);

    std::cout << "recv_count:" << recv_count << std::endl;

    std::cout << " " <<  robject.id_ << " " << robject.group_id_ << " " << robject.state_ << std::endl;

    for(int i = 0; i < robject.pos_list_.size(); i++)
    {
        std::cout << " " << robject.pos_list_.at(i).lon << " " << robject.pos_list_.at(i).lat << " " << robject.pos_list_.at(i).alt << std::endl;
    }


    return 0;
}
